package ferp.center.server.entity;

import com.google.appengine.api.datastore.Key;

import javax.persistence.*;

/**
 * User: igorgok
 * Date: 5/8/12 1:45 PM
 *
 * Ratings are kept per configuration
 */
@NamedQueries({
    @NamedQuery(name = Rating.QUERY_GET_TOP_BY_CONFIG,
                query = "SELECT r FROM Rating r WHERE r.configuration = ?1 ORDER BY r.value DESC"),
    @NamedQuery(name = Rating.QUERY_GET_ALL,
                query = "SELECT r FROM Rating r")
    })

@Entity
public class Rating
{
  public static final int UPDATE = 1;

  // get K top ratings starting from Nth
  public static final String QUERY_GET_TOP_BY_CONFIG = "getTopByConfig";
  // get all ratings
  public static final String QUERY_GET_ALL = "getAllRatings";

  //public long calculate() {return 100 * getCash() / getPool();}
  public long calculate() {return getCash();}

  public Key getKey() {return key;}
  public Profile getProfile() {return profile;}
  public void setProfile(Profile profile) {this.profile = profile;}
  public String getConfiguration() {return configuration;}
  public void setConfiguration(String configuration) {this.configuration = configuration;}
  public int getSpeed() {return speed;}
  public void setSpeed(int resolverSpeed) {this.speed = resolverSpeed;}
  public long getGames() {return games;}
  public void setGames(long games) {this.games = games;}
  public long getPool() {return pool;}
  public void setPool(long pool) {this.pool = pool;}
  public long getCash() {return cash;}
  public void setCash(long cash) {this.cash = cash;}
  public long getValue() {return value;}
  public void setValue(long value) {this.value = value;}
  public Range getRange(EntityManager em) {return range == null ? null : em.find(Range.class, range);}
  public Key getRange() {return range;}
  public void setRange(Range range) {this.range = (range == null ? null : range.getKey());}

  public Integer getUpdate() {return update;}
  public void setUpdate() {this.update = UPDATE;}

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Key key;
  // parent profile
  @ManyToOne(optional = false)
  private Profile profile;
  // game configuration
  private String configuration;
  // resolver speed (positions/sec)
  private int speed;
  // total games played
  private long games;
  // total pool
  private long pool;
  // total cash
  private long cash;
  // the actual rating value
  private long value;
  // leaf range key in the ranking tree
  private Key range;

  // update information
  private Integer update = UPDATE;

  @Version
  private int version;
}
